/*
 * $Id: ASCLogger.java,v 1.4 2009/04/10 17:29:09 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common;

import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.log4j.Logger;

import com.ge.healthcare.autosc.common.dbLogger.StepLogger;

/**
 * Purpose:
 * <ul>
 * <li>The Class provides an interface for logging</li>
 * </ul>
 * 
 * @author Navdeep Mahajan
 * @copyright (C) 2006 The General Electric Company
 */
public class ASCLogger {
	private static final String METHOD_MES_SEP = "() : ";

	private static final String CLASS_METHOD_SEP = ".";

	/**
	 * Logger class object
	 */
	private static Logger logger = null;

	private static Log4JLogger log4j = null;

	static {
		logger = Logger
				.getLogger(com.ge.healthcare.autosc.common.ASCLogger.class);
		log4j = new Log4JLogger(logger);
	}

	/**
	 * @return - boolean indicating debug logging is enabled or not
	 */
	public static boolean isDebugEnabled() {
		return log4j.isDebugEnabled();
	}

	/**
	 * @return - boolean indicating info logging is enabled or not
	 */
	public static boolean isInfoEnabled() {
		return log4j.isInfoEnabled();
	}

	/**
	 * @return - boolean indicating trace logging is enabled or not
	 */
	public static boolean isTraceEnabled() {
		return log4j.isTraceEnabled();
	}

	/**
	 * @return - boolean indicating warn logging is enabled or not
	 */
	public static boolean isWarnEnabled() {
		return log4j.isWarnEnabled();
	}

	/**
	 * @return - boolean indicating error logging is enabled or not
	 */
	public static boolean isErrorEnabled() {
		return log4j.isErrorEnabled();
	}

	/**
	 * @return - boolean indicating fatal logging is enabled or not
	 */
	public static boolean isFatalEnabled() {
		return log4j.isFatalEnabled();
	}

	/**
	 * @param message
	 */
	public static void debug(Object message) {
		debug(message, false);
	}

	/**
	 * Adds step log to the database
	 * 
	 * @param message
	 */
	private static void addStepLog(Object input) {
		String message = input.toString();
		if (message.indexOf(METHOD_MES_SEP) != -1) {
			message = message.substring(message.indexOf(METHOD_MES_SEP)
					+ METHOD_MES_SEP.length());
		}
		StepLogger.addStepLog(message);
	}

	/**
	 * @param message
	 */
	public static void debug(Object message, boolean addStepLog) {
		logger.debug(message);
		if (addStepLog) {
			addStepLog(message);
		}
	}

	/**
	 * @param message
	 */
	public static void debug(Object message, Throwable t) {
		debug(message, t, false);
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void debug(Object message, Throwable t, boolean addStepLog) {
		logger.debug(message, t);
		if (addStepLog) {
			addStepLog(new StringBuffer().append(message).append(" Exception:")
					.append(t));
		}
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 */
	public static void debug(Class<?> className, String methodName,
			Object message) {
		debug(className, methodName, message, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param addStepLog
	 */
	public static void debug(Class<?> className, String methodName,
			Object message, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		debug(logMessage, addStepLog);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 * @param addStepLog
	 */
	public static void debug(Class<?> className, String methodName,
			Object message, Throwable t) {
		debug(className, methodName, message, t, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 * @param addStepLog
	 */
	public static void debug(Class<?> className, String methodName,
			Object message, Throwable t, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		debug(logMessage, t, addStepLog);
	}

	/**
	 * @param message
	 */
	public static void info(Object message) {
		info(message, false);
	}

	/**
	 * @param message
	 */
	public static void info(Object message, boolean addStepLog) {
		logger.info(message);
		if (addStepLog) {
			addStepLog(message);
		}
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void info(Object message, Throwable t) {
		info(message, t, false);
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void info(Object message, Throwable t, boolean addStepLog) {
		logger.info(message, t);
		if (addStepLog) {
			addStepLog(new StringBuffer().append(message).append(" Exception:")
					.append(t));
		}
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 */
	public static void info(Class<?> className, String methodName,
			Object message) {
		info(className, methodName, message, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param addStepLog
	 */
	public static void info(Class<?> className, String methodName,
			Object message, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		info(logMessage, addStepLog);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 */
	public static void info(Class<?> className, String methodName,
			Object message, Throwable t) {
		info(className, methodName, message, t, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 * @param addStepLog
	 */
	public static void info(Class<?> className, String methodName,
			Object message, Throwable t, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		info(logMessage, t, addStepLog);
	}

	/**
	 * @param message
	 */
	public static void warn(Object message) {
		warn(message, false);
	}

	/**
	 * @param message
	 */
	public static void warn(Object message, boolean addStepLog) {
		logger.warn(message);
		if (addStepLog) {
			addStepLog(message);
		}
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void warn(Object message, Throwable t) {
		warn(message, t, false);
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void warn(Object message, Throwable t, boolean addStepLog) {
		logger.warn(message, t);
		if (addStepLog) {
			addStepLog(new StringBuffer().append(message).append(" Exception:")
					.append(t));
		}
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 */
	public static void warn(Class<?> className, String methodName,
			Object message) {
		warn(className, methodName, message, false);
	}

	public static void warn(Class<?> className, String methodName,
			Object message, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		warn(logMessage, addStepLog);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 */
	public static void warn(Class<?> className, String methodName,
			Object message, Throwable t) {
		warn(className, methodName, message, t, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 * @param addStepLog
	 */
	public static void warn(Class<?> className, String methodName,
			Object message, Throwable t, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		warn(logMessage, t, addStepLog);
		if (addStepLog) {
			StepLogger.addStepLog(new StringBuffer().append(message).append(
					" Exception:").append(t));
		}
	}

	/**
	 * @param message
	 */
	public static void error(Object message) {
		error(message, false);
	}

	/**
	 * @param message
	 */
	public static void error(Object message, boolean addStepLog) {
		logger.error(message);
		if (addStepLog) {
			addStepLog(message);
		}
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void error(Object message, Throwable t) {
		error(message, t, false);
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void error(Object message, Throwable t, boolean addStepLog) {
		logger.error(message, t);
		if (addStepLog) {
			addStepLog(new StringBuffer().append(message).append(" Exception:")
					.append(t));
		}
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 */
	public static void error(Class<?> className, String methodName,
			Object message) {
		error(className, methodName, message, false);
	}

	public static void error(Class<?> className, String methodName,
			Object message, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		error(logMessage, addStepLog);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 */
	public static void error(Class<?> className, String methodName,
			Object message, Throwable t) {
		error(className, methodName, message, t, false);
	}

	public static void error(Class<?> className, String methodName,
			Object message, Throwable t, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		error(logMessage, t, addStepLog);
	}

	/**
	 * @param message
	 */
	public static void fatal(Object message) {
		fatal(message, false);
	}

	/**
	 * @param message
	 */
	public static void fatal(Object message, boolean addStepLog) {
		logger.fatal(message);
		if (addStepLog) {
			addStepLog(message);
		}
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void fatal(Object message, Throwable t) {
		fatal(message, t, false);
	}

	/**
	 * @param message
	 * @param t
	 */
	public static void fatal(Object message, Throwable t, boolean addStepLog) {
		logger.fatal(message, t);
		if (addStepLog) {
			addStepLog(new StringBuffer().append(message).append(" Exception:")
					.append(t));
		}
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 */
	public static void fatal(Class<?> className, String methodName,
			Object message) {
		fatal(className, methodName, message, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param addStepLog
	 */
	public static void fatal(Class<?> className, String methodName,
			Object message, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		fatal(logMessage, addStepLog);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 */
	public static void fatal(Class<?> className, String methodName,
			Object message, Throwable t) {
		fatal(className, methodName, message, t, false);
	}

	/**
	 * @param className
	 * @param methodName
	 * @param message
	 * @param t
	 * @param addStepLog
	 */
	public static void fatal(Class<?> className, String methodName,
			Object message, Throwable t, boolean addStepLog) {
		StringBuffer logMessage = new StringBuffer();
		logMessage.append(className.getName()).append(CLASS_METHOD_SEP).append(
				methodName).append(METHOD_MES_SEP).append(message.toString());
		fatal(logMessage, t, addStepLog);
	}
}
